package com.yanyilu.basing.service.impl;

import com.yanyilu.basing.common.R;
import com.yanyilu.basing.entity.Activity;
import com.yanyilu.basing.entity.OrderForm;
import com.yanyilu.basing.entity.UnboxRecord;
import com.yanyilu.basing.entity.User;
import com.yanyilu.basing.mapper.ActivityMapper;
import com.yanyilu.basing.mapper.OrderFormMapper;
import com.yanyilu.basing.mapper.UnboxRecordMapper;
import com.yanyilu.basing.mapper.UserMapper;
import com.yanyilu.basing.service.IUnboxRecordService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yanyilu.basing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-26
 */
@Service
public class UnboxRecordServiceImpl extends ServiceImpl<UnboxRecordMapper, UnboxRecord> implements IUnboxRecordService {
    @Autowired
    UnboxRecordMapper unboxRecordMapper;

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderFormMapper orderFormMapper;

    /**
     * 添加开箱记录
     *
     * @param unboxRecord
     * @return
     */
    @Override
    public R addRecord(UnboxRecord unboxRecord) {
        UnboxRecord unboxRecord1 = unboxRecordMapper.selectByUserId(unboxRecord.getUserId());
        User user = userMapper.selectById(unboxRecord.getUserId());
        if (0 == user.getSignUpState()) {
            return R.ok("您未在规定时间内报名，暂不可参加活动！");
        }
        if (0 == user.getIsReceive()) {
            return R.error("还未确认收货，暂不能开箱！");
        }
        if (null == unboxRecord1) {
            //设置审核状态
            unboxRecord.setReviewState(1);
            Integer insert = unboxRecordMapper.insert(unboxRecord);
            user.setIsUnbox(1);
            userMapper.updateById(user);
            return insert == 1 ? R.ok() : R.error();
        }
        return R.ok();
    }

    /**
     * 获取开箱记录
     *
     * @param userId
     * @return
     */
    @Override
    public R getRecord(Integer userId) {
        UnboxRecord unboxRecord = unboxRecordMapper.selectByUserId(userId);
        return null != unboxRecord ? R.ok().put("unboxRecord", unboxRecord) : R.error("暂未获取到数据");
    }
}
